package com.gsbelarus.gedemin.salary.fragment.payslip;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.gsbelarus.gedemin.lib.ui.fragment.FragmentModel;
import com.gsbelarus.gedemin.salary.App;
import com.gsbelarus.gedemin.salary.R;
import com.gsbelarus.gedemin.salary.adapter.PayslipGeneralAdapter;
import com.gsbelarus.gedemin.salary.database.RealmHelper;
import com.gsbelarus.gedemin.salary.entity.HorizontalBarData;
import com.gsbelarus.gedemin.salary.entity.PayslipGeneralItem;
import com.gsbelarus.gedemin.salary.entity.model.PayslipItemModel;
import com.gsbelarus.gedemin.salary.entity.model.PayslipModel;
import com.gsbelarus.gedemin.salary.ui.PayslipGeneralDecoration;
import com.gsbelarus.gedemin.salary.util.DateFormatHelper;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import io.realm.Realm;
import io.realm.RealmResults;

public class PayslipGeneralFragment extends FragmentModel implements PageTitleInterface {

    private Calendar date;
    private PayslipGeneralAdapter adapter;

    private RealmHelper realmHelper;

    @Override
    public int getViewResource() {
        return R.layout.payslip_general;
    }

    @Override
    public int getPageTitleResource() {
        return R.string.payslip_general;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        realmHelper = new RealmHelper(Realm.getInstance(context));
        adapter = new PayslipGeneralAdapter(context, realmHelper);

        date = Calendar.getInstance();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        realmHelper.getRealm().close();
    }

    @Override
    public void onCreateViewFragment(View rootView) {
        super.onCreateViewFragment(rootView);

        RecyclerView generalRecView = (RecyclerView) rootView.findViewById(R.id.payslip_recycler_view);
        generalRecView.setLayoutManager(new LinearLayoutManager(context));
        generalRecView.addItemDecoration(new PayslipGeneralDecoration(context, 2f));
        generalRecView.setAdapter(adapter);

        if (isContainsParams()) {
            date.set(argumentsHelper.selectedYear, argumentsHelper.selectedMonth - 1, 1);
        }
    }

    @Override
    public void readDataBase() {

    }

    @Override
    public void initView(View view) {
        adapter.clear();
        if (!isContainsParams()) {
            List<Calendar> possibleDates = realmHelper.getPayslipPossibleDatesList();
            date = possibleDates.get(possibleDates.size() - 1);
        }
        PayslipModel payslipModel = realmHelper.getPayslip(date);

        if (payslipModel != null) {
            List<PayslipGeneralItem> taxesList = new ArrayList<>();
            List<HorizontalBarData> barData = new ArrayList<>();
            double prepaymentSum;
            double totalDebitSum;
            double totalCreditSum;
            double totalTaxesSum;

            prepaymentSum = realmHelper.getPrepaymentSum(payslipModel);
            RealmResults<PayslipItemModel> taxList = realmHelper.getTaxesList(payslipModel);
            RealmResults<PayslipItemModel> debitList = realmHelper.getDebitList(payslipModel);
            RealmResults<PayslipItemModel> creditList = realmHelper.getCreditList(payslipModel);

            totalDebitSum = debitList.sum("debit").doubleValue();
            totalCreditSum = creditList.sum("credit").doubleValue();
            totalTaxesSum = taxList.sum("credit").doubleValue();

            for (PayslipItemModel i : taxList)
                taxesList.add(new PayslipGeneralItem(i.getName(), i.getCredit(), R.color.tax_color));

            barData.add(new HorizontalBarData(R.color.debit_color, (long) (totalDebitSum - totalCreditSum - prepaymentSum - totalTaxesSum)));
            barData.add(new HorizontalBarData(R.color.credit_color, (long) totalCreditSum));
            barData.add(new HorizontalBarData(R.color.prepayment_color, (long) prepaymentSum));
            barData.add(new HorizontalBarData(R.color.tax_color, (long) totalTaxesSum));

            adapter.setDate(date)
                    .addItem(new PayslipGeneralItem(DateFormatHelper.getMonthString(context, date), "Зарплата", totalDebitSum, PayslipGeneralItem.NO_COLOR))
                    .addItem(new PayslipGeneralItem("Чистыми", totalDebitSum - totalCreditSum - prepaymentSum - totalTaxesSum, R.color.debit_color))
                    .addItem(new PayslipGeneralItem("Удержания", totalCreditSum, R.color.credit_color))
                    .addItem(new PayslipGeneralItem("Аванс", prepaymentSum, R.color.prepayment_color))
                    .addItem(new PayslipGeneralItem("Налоги", totalTaxesSum, PayslipGeneralItem.NO_COLOR))
                    .addItems(taxesList);

            adapter.showHeader("К выдаче", totalDebitSum - totalCreditSum - totalTaxesSum, barData);

        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Tracker tracker = ((App) getActivity().getApplication()).getTracker(App.TrackerName.APP_TRACKER);
        tracker.setScreenName(getClass().getSimpleName());
        tracker.send(new HitBuilders.AppViewBuilder().build());
    }
}
